package com.majorty.mail.controller;

import com.majorty.mail.controller.data.MailRequest;
import com.majorty.mail.service.EmailService;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhangshuai
 * @date 2019-08-31 16:19
 */
@Controller
@RequestMapping(value = "/mail")
public class EmailController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(EmailController.class);

    @Autowired
    private EmailService emailService;

    @ResponseBody
    @RequestMapping(value = { "/sendMail" })
    public String sendEmail(HttpServletRequest req, HttpServletResponse resp, String sessionId) {
        MailRequest request = ParseMsg(req,MailRequest.class);
        if (request == null){
            return "请求参数为空！";
        }
        if (StringUtils.isNotEmpty(request.getContext())){
            //普通邮件
            emailService.sendMail(request.getTitle(),
                    request.getMailToAddrs(),
                    request.getMailCopyToAddrs(),
                    request.getContext(),
                    request.getFileAttachList());
        }else {
            //模板邮件
            emailService.sendMail(request.getTitle(),
                    request.getMailToAddrs(),
                    request.getMailCopyToAddrs(),
                    request.getTemplatePath(),
                    request.getModel(),
                    request.getFileAttachList());
        }
        return "成功！";
    }
}
